<?php

namespace Zk\UserBundle\Menu;

use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\Security\Core\SecurityContext;

class Builder extends ContainerAware
{
    protected $factory;
    
    protected $user;
    
    protected $menu;
    
    protected $translator;
    
    protected $securityContext;
    
    protected $request;
    
    protected $options;
    
    /**
     * @param FactoryInterface $factory
     */
    public function __construct(FactoryInterface $factory)
    {
        $this->factory = $factory;
    }

    public function createMainMenu(
                                   Request $request,
                                   Translator $translator,
                                   SecurityContext $securityContext,
                                   array $options=array()
    )
    {
        $this->translator = $translator;
    
        $this->securityContext = $securityContext;
    
        $this->request = $request;
        
        $this->options = $options;
       
        $this->user = ( $this->securityContext->getToken()
                       and $this->securityContext->getToken()->getUser() instanceof \Zk\UserBundle\Entity\User )
        ? $this->securityContext->getToken()->getUser()
        : null;
        
        $this->menu = $this->factory->createItem('root'); 
        
        $item = $this->menu->setChildrenAttribute('class', 'pull-right');
        
        $this->getLangMenu();
        
        $this->getProfileMenu();
        #exit(print_r($this->menu->getRoot()->toArray())); 
        return $this->menu;
    }

    /**
     * Get menu "Profile"
     */
    public function getProfileMenu()
    {
        if( $this->user )
        {
            $profile = $this->menu->addChild('profile', array('route' => 'zk_admin_homepage'))
            ->setAttribute('class', 'dropdown')
            ->setLabel('.icon-user '.$this->user->getEmail());
            
            $profile->addChild('profile_show', array(
                'route' => 'fos_user_profile_show'
            ))
            ->setLabel('.icon-user '.$this->translator->trans('main.profile',array(),'sfMenu'));
            
            $profile->addChild('password_change', array(
                'route' => 'fos_user_change_password'
            ))
            ->setLabel('.icon-refresh '.$this->translator->trans('main.change_password',array(),'sfMenu'));
            
            $profile->addChild('profile_d1', array('attributes' => array('divider' => true)));
            
            if( $this->user->flagSuperAdmin() or $this->securityContext->isGranted( array('ROLE_USER_LIST') ) )
            {
                $profile->addChild('zk_admin_user_list', array(
                    'route' => 'zk_admin_user_list'
                ))
                ->setLabel('.icon-tasks '.$this->translator->trans('main.user_list',array(),'sfMenu'));
            }
            
            if( $this->user->flagSuperAdmin() or $this->securityContext->isGranted( array('ROLE_ROLE_LIST') ) )
            {
                $profile->addChild('zk_admin_role_list', array(
                    'route' => 'zk_admin_role_list'
                ))
                ->setLabel('.icon-tasks '.$this->translator->trans('main.role_list',array(),'sfMenu'));
            }
            
            if( $this->user->flagSuperAdmin() or $this->securityContext->isGranted( array('ROLE_GROUP_LIST') ) )
            {

                $profile->addChild('zk_admin_group_list', array(
                    'route' => 'zk_admin_group_list'
                ))
                ->setLabel('.icon-tasks '.$this->translator->trans('main.group_list',array(),'sfMenu'));
                
                $profile->addChild('profile_d2', array('attributes' => array('divider' => true)));
            }
            
            $profile->addChild('logout', array(
                'route' => 'logout'
            ))
            ->setLabel('.icon-road '.$this->translator->trans('main.logout',array(),'sfMenu'));
        }
        else
        {
            $this->menu->addChild('login', array(
                'route' => 'login'
            ))
            ->setLabel('.icon-lock '.$this->translator->trans('main.login',array(),'sfMenu'));
        }
    }
    
    /**
     * Get menu "Select language"
     */
    public function getLangMenu()
    {
        if( !isset($this->options['locale']) or !$this->options['locale'] ) return;
      
        $lang = $this->menu->addChild('lang', array('uri' => '#'))
        ->setAttribute('class', 'dropdown')
#        ->setLabel( '<img style="margin-top:1px;" src="/img/lang_ru_min.png"> RU' );
        ->setLabel( '<img style="margin-top:-3px;" src="/img/lang_'.strtolower($this->request->getLocale()).'_min.png"> '.strtoupper($this->request->getLocale()) );
        $lang->addChild('en_locale', array(
#            'uri' => '#',
            'route' => 'zk_set_locale',
            'routeParameters' => array('locale' => 'en')
        ))
        ->setLabel('<img src="/img/lang_en_min.png"> English');
        $lang->addChild('ru_locale', array(
#            'uri' => '#',
            'route' => 'zk_set_locale',
            'routeParameters' => array('locale' => 'ru')
        ))
        ->setLabel('<img src="/img/lang_ru_min.png">  Русский');
    }

}